/**
 * Private Variable Declaration
 * */
 import Modules.Security.UserDetail;
 import Modules.Security.UserInfo;
 import Modules.Windows.AdminWindow;
 
 import com.desidea.components.grid.SuperGridEvent;
 import com.desidea.components.grid.button.SuperGridButton;
 import com.desidea.utils.DirtyChecker;
 import com.desidea.utils.IDirtyChecker;
 import com.desidea.webservice.PartnerService;
 import com.desidea.webservice.ProcessServiceRequestResultEvent;
 
 import flash.events.Event;
 
 import mx.controls.Alert;
 import mx.controls.Button;
 import mx.controls.dataGridClasses.DataGridColumn;
 import mx.managers.PopUpManager;
 import mx.rpc.events.FaultEvent;
 
/**
 * Event Handling - CreationComplete event
 * */
private function windowCreated():void{
	
	var newItemBtn:Button = new Button();
	newItemBtn.label = "Create New User";
	newItemBtn.addEventListener(MouseEvent.CLICK, function(event:Event):void{
														showDetail(SuperGridButton.MODE_EDIT,"");
													});

//showDetail(SuperGridButton.MODE_VIEW);
	usersGrid.toolBar.addChild(newItemBtn);
	
	this.usersGrid.addEventListener(SuperGridEvent.CLICKED, gridButtonColumnClicked);

	loadData();
}

private function onButtonClick(event:Event, identifier:String = ""):void{
	switch(identifier){
		case "new":
			displayUserDetailPage();
			break;
	}
}

private function displayUserDetailPage(info:UserInfo = null):void{
	if(info == null || info.id <= 0){
		var win:AdminWindow = this.parentDocument as AdminWindow;
		win.removeAllChildren();
		
		var detail:UserDetail = new UserDetail();
//		detail.mode = "edit";
		win.addChild(detail);
		
	}else{
		//TODO: Display Account Details
	}
}

public function loadData():void{
	if( DirtyChecker.getInstance().isDirty){
		if (DirtyChecker.getInstance().sourceContainer is IDirtyChecker){
			(DirtyChecker.getInstance().sourceContainer as IDirtyChecker).promptToSave();
		}else{
			Alert.show("Save & Proceed");
		}
		return;
	}else{
		DirtyChecker.getInstance().resetDirtyChecker();
	}
	
	var params:String = "OPERATION=USERS_LIST" +
						"&ISACTIVE=X";
//						"&USERID="+CommonClass.UserID;
	
	var ws:PartnerService = new PartnerService();
	ws.addprocessServiceRequestEventListener(callForMyContactsList);
	ws.addPartnerServiceFaultEventListener(partnerServiceFaultHandler);
	ws.processServiceRequest(params);
	
//	BusySpinner.getInstance().play(Application.application.ModuleCvs);

}

private function callForMyContactsList(event:ProcessServiceRequestResultEvent):void{
//	BusySpinner.getInstance().stop();	
	var response:XML = XML(event.result);

	lblError.text = "";
	if(response.RESPONSE.RESULT == "E"){
		lblError.text = response.RESPONSE.MESSAGE
	}else if(response.RESPONSE.RESULT == 'S'){
		this.usersGrid.dataProvider = response;
	}
	setColumnWidths();
}

private function partnerServiceFaultHandler(event:FaultEvent):void{
//	BusySpinner.getInstance().stop();
	Alert.show(event.fault.toString());
}

private function setColumnWidths():void{
	setColumnWidth(1, 80, "right");
	setColumnWidth(2, 150);
	setColumnWidth(3, 100);
	setColumnWidth(4, 200);
	setColumnWidth(5, 140,"center");
	setColumnWidth(6, 120,"center");
	setColumnWidth(7, 90,"center");
	setColumnWidth(8, 90,"center");
}

private function setColumnWidth(colIndex:int, colWidth:int, alignment:String = "left"):void{
	if(this.usersGrid.GridComponent.columnCount > colIndex)	
		this.usersGrid.GridComponent.columns[colIndex].width = colWidth;
		
		var col:DataGridColumn = this.usersGrid.GridComponent.columns[colIndex];
		col.setStyle("textAlign", alignment);
}


private function gridButtonColumnClicked(event:SuperGridEvent):void{
	handleGridButtonClick(event);
}

private function handleGridButtonClick(event:SuperGridEvent, moduleCode:String = null):void{
	var mode:String =  String(event.data.linkId).toLowerCase();
	
	if(mode == SuperGridButton.MODE_EDIT || mode == SuperGridButton.MODE_VIEW || mode == SuperGridButton.MODE_VIEWICON){
		showDetail(mode, event.data.data["key"].toString());
	}else{
		Alert.show("Mode : " + mode + " not handled");
	}
}

private function showDetail(mode:String, key:String=""):void{
	var cd:UserDetail = new UserDetail();
	cd.key = key;
	cd.caller = this;
	
	PopUpManager.addPopUp(cd,this,true);
	PopUpManager.centerPopUp(cd);
}